/**
 * 系统用户修改js
 */
var comValidform=null;
var returnStatus=false;
var sysUserMutiUpdateOrg={
	dataCache:null,
	duties:null,
	selectUserIds:[],
	hasError:false,
	ivt:null,
	time:10,
	duties:null,

	startEvent:function(){
		var parms ="";
		var checkResult=true;

		//更改职务提交
		comValidform=$(".com-addForm").Validform({
			tipSweep:true,

			beforeCheck:function(curforem){

				if(sysUserMutiUpdateOrg.hasError  || sysUserMutiUpdateOrg.time > 0){
					return false;
				}

				var chgCustomer=$("input:radio[name='chgCustomer']:checked").val() ;

				params = {
					'ids': sysUserMutiUpdateOrg.selectUserIds.toString(),
					'orgId': $("#orgId").val().trim(),
					'actUserId': userId,
					'chgCustomer': chgCustomer
				}

				if(params.actUserId == null){
					sysUserMutiUpdateOrg.hasError = true;
					console.log("登录超时");
					$("#errorTr").css("display", '');
					$("#errorMsg").html("登录超时，请重新登录后尝试！");
					return false;
				}

				console.log(params.orgId);

				if ( params.orgId == null) {
					sysUserMutiUpdateOrg.hasError = true;
					$("#errorTr").css("display", '');
					$("#errorMsg").html("组织架构ID为空，请重试！");
					return false;
				}


				$("#errorTr").css("display", 'none');
				$("#errorMsg").html("");




				var mymessage= "";

				if(params.chgCustomer == 1){
					mymessage = confirm("将推荐码为已修改理财师的客户也转移到新部门，是否继续？");
				}
				
				return mymessage;




			},
			beforeSubmit:function(curform){
				console.log("in before submit function");
				$("#msgTd").html("操作进行中，请等待······");

				url = 'mgr/SysUserCtrl/mutiUpdateSysUserOrg';

				var cb = function(data){

					//成功返回
					if(data.resultCode==200){
						$("#msgTd").html("修改成功！");
						//关闭窗口并刷新数据
						alert("修改成功！");
						returnStatus=true;
					}else{
						$("#msgTd").html("");
						$("#errorTr").css("display", '');
						$("#errorMsg").html(data.msg);
					}
				};
				sendApiNoLoadSync(url, params, cb, 'POST');
				return returnStatus;
			}

		});
	},

	getResource:function(){
		//获取所有职务
		var url = 'mgr/OrganizationCtrl/listAllNormal';
		var params = null;

		var cb = function(data) {
			console.trace("data code = " & data.resultCode);
			if (data.resultCode == 200) {
				sysUserMutiUpdateOrg.duties = data.result;
				sysUserMutiUpdateOrg.showDuties();
			}
		};
		sendApiNoLoadSync(url, params, cb, 'GET');
	},
	showDuties:function(){
		var setting = {
			check : {
				enable : true,
				chkStyle : "radio",
				radioType : "all"
			},
			data : {
				simpleData : {
					enable : true
				}
			},
			view : {
				nameIsHTML : true
			},
			callback : {
				onCheck : zTreeOnClick,
				beforeCheck: zTreeBeforeCheck
			}
		};
		var zNodes = [];
		var duties = sysUserMutiUpdateOrg.duties;

		var orgId=$('#com-addForm input[name="orgId"]').val();
		for (var i = 0; i < duties.length; i++) {
			zNodes[i] = {
				id : duties[i].id,
				name : duties[i].name,
				pId : duties[i].parentID,
				orgFullName: duties[i].flag02,
			};
			if (i == 0) {
				zNodes[i].open = true;
			}

			if(orgId == duties[i].id){
				zNodes[i].checked=true;
			};
		}
		$.fn.zTree.init($("#dutTree"), setting, zNodes);
	},
	showSelectedUsers:function(){
		var selectedUsers=art.dialog.data('selectedUsers');  //修改id

		sysUserMutiUpdateOrg.selectUserIds = selectedUsers;

		var tr ="";

		sysUserMutiUpdateOrg.hasError = false;

		for(var i = 0; i < selectedUsers.length; i++){
			var userId = selectedUsers[i];
			var user = eval('(' + localStorage.getItem(userId + "_User_Key") + ')');

			tr += '<tr><td>'
					+ (i+1)
					+ '</td><td>'
					+ user.realName
					+ '</td><td>'
					+ user.name
					+ '</td></tr>'
		}
		$('#updUserList tbody').html(tr);

		$("#errorTr").css("display", 'none');
		$("#errorMsg").html("");

		if(!sysUserMutiUpdateOrg.hasError ){
			sysUserMutiUpdateOrg.itv = setInterval(function(){
				$("#errorTr").css("display", '');
				$("#errorMsg").html("<h2>请在" + sysUserMutiUpdateOrg.time +  "秒后进行操作</h2>");

				sysUserMutiUpdateOrg.time = sysUserMutiUpdateOrg.time - 1;

				if(sysUserMutiUpdateOrg.time < 0) {
					clearInterval(sysUserMutiUpdateOrg.itv);
					$("#errorTr").css("display", 'none');
				}

			}, 1000);
		}
	},
	
	init:function(){
		sysUserMutiUpdateOrg.getResource();
		sysUserMutiUpdateOrg.showSelectedUsers();
		sysUserMutiUpdateOrg.startEvent();
		sysUserMutiUpdateOrg.hasError = false;
	}
}
$(document).ready(function() {
	sysUserMutiUpdateOrg.init();
});

function submitForm(){
	comValidform.submitForm();
	return returnStatus;
}


function zTreeOnClick(event, treeId, treeNode) {
	$("#orgId").val(treeNode.id);
};

function zTreeBeforeCheck(treeId, treeNode, clickFlag) {
	console.log("treeNode.orgShow = " + treeNode.orgFullName);
	$("#orgShow").html(treeNode.orgFullName);
	if(treeNode.isParent){
		alert("请选择具体的职务！");
		$("#errorTr").css("display", '');
		$("#errorMsg").html("所选的不是职务");
		sysUserMutiUpdateOrg.hasError = true;
	}else{
		$("#errorTr").css("display", 'none');
		$("#errorMsg").html("");
		sysUserMutiUpdateOrg.hasError = false;
	}

	return !treeNode.isParent;//当是父节点 返回false 不让选取
};